package com.jzg.jcptadmin.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.base.BaseActivity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 选择时间界面
 *日期选择，年月日
 * @author 作者 E-mail:
 * @version 创建时间：
 */
@SuppressLint("NewApi")
public class SheetdayTimeAty extends BaseActivity {


    int win_width;
    int win_height;
    @Bind(R.id.time_title)
    TextView timeTitle;
    @Bind(R.id.yearNumberPicker)
    NumberPicker yearNumberPicker;
    @Bind(R.id.monthNumberPicker)
    NumberPicker monthNumberPicker;
    @Bind(R.id.dial_layout)
    LinearLayout dialLayout;
    @Bind(R.id.dayNumberPicker)
    NumberPicker dayNumberPicker;


    private int maxYear;
    private int minYear;

    private final int maxMonth = 12;
    private final int minMonth = 1;
    private int maxYearMonth = 12;
    private int minYearMonth = 1;

    private int currentYear;
    private int currentMonth;
    String[] yearShow = null;
    private int currentDay;
    private int maxDay = 31;
    private int minDay = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setFinishOnTouchOutside(true);

        setContentView(R.layout.act_day_sheettime);
        ButterKnife.bind(this);
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
        WindowManager.LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
        Point size = new Point();
        d.getSize(size);
        win_width = size.x;
        win_height = size.y;
        p.gravity = Gravity.BOTTOM;
        p.height = (int) (win_height * 0.5); // 高度设置为屏幕的0.6
        p.width = (int) (win_width * 1); // 宽度设置为屏幕的1
        p.alpha = 1.0f;// 设置透明度
        Intent intent = getIntent();
        maxYearMonth = intent.getIntExtra("MaxMonth", 12);
        minYearMonth = intent.getIntExtra("curMonth", 1);
        yearShow = intent.getStringArrayExtra("yearShow");
        maxYear = yearShow.length-1;
        minYear = 0;
        String title = intent.getStringExtra("title");
        if (!TextUtils.isEmpty(title)) {
            timeTitle.setText(title);
        }

        this.getWindow().setAttributes(p);
        currentYear = maxYear;            //默认显示最大年限
        currentMonth = minYearMonth;
        currentDay = minDay;
        yearNumberPicker.setDisplayedValues(yearShow);
        yearNumberPicker.setMaxValue(maxYear);
        yearNumberPicker.setMinValue(minYear);
        yearNumberPicker.setValue(currentYear);

        monthNumberPicker.setMaxValue(maxYearMonth);
        monthNumberPicker.setMinValue(minMonth);
        monthNumberPicker.setValue(currentMonth);

        dayNumberPicker.setMaxValue(30);
        dayNumberPicker.setMinValue(1);
        dayNumberPicker.setValue(1);

        yearNumberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        monthNumberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        yearNumberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                int year = picker.getValue();
                currentYear = year;
                updateMonth();
            }
        });
        monthNumberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {

            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                int month = picker.getValue();
                currentMonth = month;
                updateMonth();
            }
        });
        dayNumberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                int day = picker.getValue();
                currentDay = day;
            }
        });
        updateMonth();

    }


    public void updateMonth(){
        //当前年
        if(yearNumberPicker.getValue() == 1){
			/*if(StringUtils.getMonth() !=1){
				monthNumberPicker.setMaxValue(StringUtils.getMonth()-1);
			}else{
				yearNumberPicker.setMaxValue(maxYear-1);
				monthNumberPicker.setMaxValue(12);
			}*/
            monthNumberPicker.setMaxValue(maxMonth);
            monthNumberPicker.setMinValue(minYearMonth);
            dayNumberPicker.setMaxValue(maxDay);
            dayNumberPicker.setMinValue(minDay);
            updateDay();
            //已过期显示
        }else if(yearNumberPicker.getValue() == minYear){
            monthNumberPicker.setMaxValue(0);
            monthNumberPicker.setMinValue(0);
            dayNumberPicker.setMaxValue(0);
            dayNumberPicker.setMinValue(0);

        }else{
            monthNumberPicker.setMaxValue(maxMonth);
            monthNumberPicker.setMinValue(minMonth);
            dayNumberPicker.setMaxValue(maxDay);
            dayNumberPicker.setMinValue(minDay);
            updateDay();
        }


    }

    public void updateDay() {
        int y = yearNumberPicker.getValue();
        int m = monthNumberPicker.getValue();
        if (is31(m)) {
            dayNumberPicker.setMaxValue(31);
            dayNumberPicker.setMinValue(1);
        } else {
            if (m == 2) {
                if(isRunNian(yearShow[y])){
                    dayNumberPicker.setMaxValue(29);
                    dayNumberPicker.setMinValue(1);
                }else{
                    dayNumberPicker.setMaxValue(28);
                    dayNumberPicker.setMinValue(1);
                }

            }else{

                dayNumberPicker.setMaxValue(30);
                dayNumberPicker.setMinValue(1);
            }

        }
    }

    /**
     * 根据月份判断是否是31天
     * @param month
     * @return
     */
    public boolean is31(int month){
        Pattern pattern = Pattern.compile("([13578]|1[02])");
        Matcher matcher = pattern.matcher(month + "");
        return  matcher.matches();
    }
    /**
     * 判断是否是闰年
     * @param year
     * @return
     */
    public boolean isRunNian(String year) {
        Pattern pattern = Pattern.compile("(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))");
        Matcher matcher = pattern.matcher(year);
        return matcher.matches();
    }

    public void time_done(View view) {
        switch (view.getId()){
            case R.id.time_done:
                back_data();
                finish();
                break;
        }
    }


    public void back_data() {
        Intent in = new Intent();
        if(yearNumberPicker.getValue() == 0){
            in.putExtra("car_Time_MSG", yearShow[yearNumberPicker.getValue()]+"");
        }else{
            in.putExtra("car_Time_MSG", yearShow[yearNumberPicker.getValue()]+"-"+monthNumberPicker.getValue()+"-"+dayNumberPicker.getValue());
        }
        setResult(RESULT_OK, in);
    }

}
